#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "jiexi.h"
#include "wrizhi.h"
enum ml get_enum(char *s) {
    if (strncmp(s, "ls", 2) == 0) {
        return ls;
    } else if (strncmp(s, "quit", 4) == 0) {
        return quit;
    } else if (strncmp(s, "get", 3) == 0) {
        return get;
    } else if (strncmp(s, "put", 3) == 0) {
        return put;
    } else if (strncmp(s, "cd", 2) == 0) {
        return cd;
    } else if (strncmp(s, "history", 7) == 0) {
        return history;
    } else {
        return erro;
    }
}
file jiexi(char *s, file ff) {
    ff.s = get_enum(s);
    strtok(s, " ");
    char *p = strtok(NULL, " ");
    if (p == NULL) {
        memset(ff.name, 0, sizeof(ff.name));
        return ff;
    }
    strcpy(ff.name, p);
    return ff;
}
void fasong(int fd, file ff) {
    char s[132] = "md5sum ";
    char *p;
    p = strcat(s, ff.name);
    FILE *fp = popen(p, "r");
    fread(ff.md5, 1, sizeof(ff.md5), fp);
    pclose(fp);
    printf("%s", ff.md5);

    fp = fopen(ff.name, "r");
    fseek(fp, 0, SEEK_END);
    ff.len = ftell(fp);
    fseek(fp, 0, SEEK_SET);
    ff.sub = (char *)malloc(sizeof(char) * ff.len);
    memset(ff.sub, 0, ff.len);
    fread(ff.sub, 1, ff.len, fp);
    fclose(fp);
    write(fd, &ff, sizeof(ff));
    write(fd, ff.sub, ff.len);
    free(ff.sub);
    ff.sub = NULL;
}
void huoqu(int fd, file ff) {
    ff.sub = (char *)malloc(sizeof(char) * ff.len);
    memset(ff.sub, 0, sizeof(ff.sub));
    read(fd, ff.sub, ff.len);
    printf("%s\n%s\n", ff.sub, ff.md5);
    FILE *fp = fopen(ff.name, "w+");
    fwrite(ff.sub, 1, ff.len, fp);
    fclose(fp);
    free(ff.sub);
    ff.sub = NULL;

    char s[132] = "md5sum ";
    char *p;
    p = strcat(s, ff.name);
    fp = popen(p, "r");
    fread(ff.md5, 1, sizeof(ff.md5), fp);
    pclose(fp);
    printf("%s", ff.md5);
}
char *zhuanghuan(enum ml sss) {
    char *p = NULL;
    p = (char *)malloc(sizeof(char) * 8);
    memset(p, 0, 8);
    switch (sss) {
        case 1:
            strcpy(p, "ls");
            break;
        case 2:
            strcpy(p, "quit");
            break;
        case 3:
            strcpy(p, "get");
            break;
        case 4:
            strcpy(p, "put");
            break;
        case 5:
            strcpy(p, "cd");
            break;
        case 6:
            strcpy(p, "history");
            break;
        default:
            strcpy(p, "erro");
            break;
    }
    return p;
}

void gethist(int fd) {
    hist p;
        printf("############################\n");
    while (1) {
        read(fd, &p, sizeof(p));
        printf("命令：%s参数：%s\n",zhuanghuan(p.hist_s),p.hist_name );
        if(p.next==NULL){
            break;
        }
    }
        printf("############################\n");
}
int chuli(int fd, file ff) {
    long i;
    file ff_hist;
    switch (ff.s) {
        case 1:
            write(fd, &ff, sizeof(ff));
            read(fd, &ff, sizeof(ff));
            ff.sub = (char *)malloc(sizeof(char) * ff.len);
            memset(ff.sub, 0, ff.len);
            read(fd, ff.sub, ff.len);
            printf("%s", ff.sub);
            wrizhi("%s", ff.sub);
            free(ff.sub);
            ff.sub = NULL;
            break;
        case 2:
            write(fd, &ff, sizeof(ff));
            close(fd);
            return -1;
            break;
        case 3:
            write(fd, &ff, sizeof(ff));
            read(fd, &ff, sizeof(ff));
            huoqu(fd, ff);
            break;
        case 4:
            fasong(fd, ff);
            break;
        case 5:
            write(fd, &ff, sizeof(ff));
            break;
        case 6:
            write(fd, &ff, sizeof(ff));
            gethist(fd);
            break;
        default:
            printf("无效命令\n");
            break;
    }
    printf("-----------------\n");
    return 0;
}
